/**
 * Created by Administrator on 2019/8/6.
 */
"use strict";
const passwordHash = require('yi-password-hash');
const async = require('async');
const ObjectID = require('mongodb').ObjectID;

const setRedisFun = (req,res,user)=>{
    let roleIds = [];
    const token = new ObjectID();
    if(!user.roleId){
        user.token=token;

        req.mongo.collection('menu').find({}).project({_id:1}).toArray((err0,menuIds)=>{
            user.menus=[];
            menuIds.forEach((item)=>{
                user.menus.push(item._id.toString());
            });

            req.mongo.collection('api').find({open:false}).project({_id:1}).toArray((err0,apiIds)=> {
                user.apis = [];
                apiIds.forEach((item) => {
                    user.apis.push(item._id.toString());
                });
                let updateQueue=[];
                let tokenExpires =  req.config.sys.tokenExpires|| 24 * 60 ;
                tokenExpires = tokenExpires * 60;
                updateQueue.push(callback=> req.redis.setValueAndExpire("token:"+user.token,JSON.stringify(user),tokenExpires,callback));

                async.auto(updateQueue,(err,  resultSet)=> {
                    if (err) return res.err.redisError(err);
                    res.reply({code:0,message:"成功",data:user});
                });
            });
        });

    }
    // roleIds.push(user.roleId);
    // req.mongo.collection('managerRole').find({"_id":{$in:roleIds}},{$project:{menu:1}}).next(
    //     function(err,result){
    //         user.token=token;
    //         const authInfo = {
    //             token: token,
    //             apis: roleIds,
    //             menus:result.menu,
    //             context: {
    //                 //上下文，暂时为空
    //             }
    //         };
    //         let updateQueue=[];
    //
    //         let tokenExpires =  req.config.sys.tokenExpires|| 24 * 60 ;
    //         tokenExpires = tokenExpires * 60;
    //         updateQueue.push(callback=> req.redis.setValueAndExpire("user:"+user._id,JSON.stringify(user),tokenExpires,callback));
    //         async.auto(updateQueue,(err,  resultSet)=> {
    //             if (err) return res.err.redisError(err);
    //             res.reply({code:0,message:"成功",data:user});
    //         });
    //     });
}
module.exports = {
    //open:true,
    //security: true,
    description:'管理员登录',
    handler(req, res, next) {
        const username = req.body.username;
        const password = req.body.password;
        req.mongo.collection('manager')
            .find({name: username})
            .limit(1)
            .next(function (err, user) {
                if (err) return res.err.mongoError(err);
                if (!user) {
                    return res.reply({code:1001,message:"账号不存在",data:null});
                }
                passwordHash.compare(password, user.pwd, function (err, isMatch) {
                    if (err) return res.err.unknownError();
                    if (!isMatch){
                        return res.reply({code:1000,message:"密码错误",data:null});
                    }
                    delete user.pwd;
                    //密码已经验证成功
                    //获取登录信息及角色菜单
                    setRedisFun(req,res,user);
                });
            });
    }
};
